/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.unice.miage.projetjava.managers;

import fr.unice.miage.projetjava.entities.Users;
import fr.unice.miage.projetjava.entities.Produit;
import fr.unice.miage.projetjava.*;
import fr.unice.miage.projetjava.entities.Commentaire;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean gerant Commentaire
 * @author Olivier
 */
@Stateless
public class GestionnaireDeCommentaireBean implements GestionnaireDeCommentaireLocal {
    @PersistenceContext
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "EJB Methods > Add Business Method" or "Web Service > Add Operation")

    public Commentaire creerCommentaire(String text, Users user, Date datePosted, int mark, Produit prod)
    {
       Commentaire comment = new Commentaire();
       comment.setText(text);
       comment.setUser(user);
       comment.setDatePosted(datePosted);
       comment.setMark(mark);
       comment.setProduit(prod);
       em.persist(comment);
       return comment;
    }

    public List<Commentaire> listeDeCommentaires()
    {
        Query query = em.createQuery("SELECT c FROM Commentaire c");
        return query.getResultList();
    }

    public List<Commentaire> listeDeCommentaires(Produit prod)
    {
        Query query = em.createQuery("SELECT c FROM Commentaire c WHERE c.produit='"+prod+"'");
        return query.getResultList();
    }
    
}
